package com.example.function_interface.operator;

import com.example.function_interface.User;
import org.junit.Test;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.function.DoubleUnaryOperator;

/**
 * 接受一个参数同为类型double,返回值类型也为double
 */
public class DoubleUnaryOperatorTest {

    @Test
    public void myTest() throws Exception{
        List<User> userList = new ArrayList<>();
        User user1 = new User(1, "张三", 12, false, 23.81);
        User user2 = new User(2, "李四", 22, false, 200);
        User user3 = new User(3, "王五", 32, true, 123.28);
        User user4 = new User(4, "郑静", 2, true, 0);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        DoubleUnaryOperator doubleUnaryOperator = new DoubleUnaryOperator() {
            @Override
            public double applyAsDouble(double operand) {
                DecimalFormat df = new DecimalFormat("#.0");
                String result = df.format(operand);
                return Double.valueOf(result);
            }
        };
        DoubleUnaryOperator doubleUnaryOperator2 = doubleUnaryOperator.compose((operand) -> operand / 10);
        DoubleUnaryOperator doubleUnaryOperator3 = doubleUnaryOperator.andThen((operand) -> operand + 10);

        System.out.println(doubleUnaryOperator2.applyAsDouble(user1.getMoney()));
        System.out.println(doubleUnaryOperator3.applyAsDouble(user1.getMoney()));
    }
}
